from urllib import request

from django.shortcuts import render
from django.views import View
from reservation.models import Reservation
from cars.models import *
from django.http import HttpResponseRedirect, HttpResponse

from users.models import User


# Create your views here.
#车型总览
def car_model_all(request,m_id=1,t_id=1):
    # 获取所有品牌
    models_list = Carmodels.objects.all().order_by('id')
    # 获取所有车类型
    types_list = Cartype.objects.all().order_by('id')
    #获取所有汽车
    cars_list = Cars.objects.filter(model_id=m_id, type_id=t_id).order_by('id')

    carsnumber=len(cars_list)
    #获取预约的信息
    user = request.session.get('user')
    if user:
        reservation_list = Reservation.objects.filter(user_id=user.id)
        number= len(reservation_list)

        return render(request, 'templates/cars/car-model-all.html', {'models_list':models_list,
                                                      'type_list':types_list,
                                                      'cars_list':cars_list,
                                                      'm_id':m_id,
                                                      't_id':t_id,
                                                      'carsnumber':carsnumber,
                                                      'number':number})
    else:
        return render(request, 'templates/cars/car-model-all.html', {'models_list':models_list,
                                                      'type_list':types_list,
                                                      'cars_list':cars_list,
                                                      'm_id':m_id,
                                                      't_id':t_id,
                                                      'carsnumber':carsnumber,
                                                                     })

#详情
class car_detail(View):
    def get(self,request,car_id=1):
        detail_list = Cardetails.objects.filter(car_id=car_id)
        elavent_list = CarEvaluate.objects.filter(Cars_id=car_id)
        car_list = Cars.objects.filter(id=car_id)
        img_list = Officeimg.objects.filter(car_id=car_id)
        cars=Cars.objects.get(id=car_id)
        model = Carmodels.objects.get(id=cars.model_id)
        user = request.session.get('user')
        if user:
            reservation_list = Reservation.objects.filter(user_id=user.id)
            number = len(reservation_list)
            return render(request, 'templates/cars/car_details.html',
                          {'car_id':car_id,
                           'detail_list':detail_list,
                           'elavent_list':elavent_list,
                           'car_list':car_list,
                           'img_list':img_list,
                           'number':number,
                           'model':model})
        else:
            return render(request, 'templates/cars/car_details.html',
                          {'car_id':car_id,
                           'detail_list':detail_list,
                           'elavent_list':elavent_list,
                           'car_list':car_list,
                           'img_list':img_list,
                           'model':model})


    def post(self,request,car_id=1):
        content = request.POST.get('userInput','')
        user = request.session.get('user')
        print(content)
        if content:
            CarEvaluate.objects.create(ename=user.name,evaluate=content,Cars_id=car_id)
        else:
            pass
        return HttpResponseRedirect('/car_model_all/cardetail/'+str(car_id))


#搜索
class car_search(View):
    def post(self,request):
        key = request.POST.get('key')
        search_list = Cars.objects.filter(cname__contains=key)
        user = request.session.get('user')
        if user:
            reservation_list = Reservation.objects.filter(user_id=user.id)
            number = len(reservation_list)
            return render(request, 'templates/cars/cars.html', {'search_list':search_list, 'number':number})
        else:
            return render(request, 'templates/cars/cars.html', {'search_list':search_list})







